package com.itany.service.impl;

import com.itany.entity.*;
import com.itany.mapper.*;
import com.itany.service.AreaService;
import com.itany.service.ExamineService;
import com.itany.service.TypeService;
import com.itany.utils.OssClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class ExamineServiceImpl implements ExamineService {
    @Autowired
    private ExamineMapper mapper;
    @Autowired
    AreaService areaService;
    @Autowired
    TypeService typeService;
    @Autowired
    AnnexMapper annexMapper;
    @Autowired
    ServerCompanyMapper serverCompanyMapper;
    @Autowired
    ServerInfoMapper serverInfoMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    private ServerTypeMapper serverTypeMapper;
    @Autowired
    private ServerAreaMapper serverAreaMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String name, Integer flag, Integer examineType) {
        List<Examine> list = mapper.selectByParams(name,flag,examineType);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()) {
            for (Examine examine : list) {
                String str1 = "";
                String str2 = "";
                List<String> list1 = new ArrayList<>();
                List<String> list2 = new ArrayList<>();
                String types = examine.getServertype();
                if(types != null && !types.isEmpty()){
                    String[] args1 = types.split(",");
                    for(String arg:args1){
                        Type type = typeService.findById(Integer.parseInt(arg));
                        if(type != null){
                            list1.add(type.getName());
                        }
                    }
                    str1 = String.join(",",list1);
                }
                String areas = examine.getServerarea();
                if(areas != null && !areas.isEmpty()){
                    String[] args2 = areas.split(",");
                    for(String arg:args2){
                        Area area = areaService.findById(Integer.parseInt(arg));
                        list2.add(area.getName());
                    }
                    str2 = String.join(",",list2);
                }
                Map<String, Object> u = new HashMap<>();
                u.put("id",examine.getId());
                u.put("title",examine.getTitle());
                u.put("name",examine.getName());
                u.put("address",examine.getAddress());
                u.put("gps",examine.getGps());
                u.put("typeid",examine.getTypeid());
                u.put("createdate",examine.getCreatedate());
                u.put("username",examine.getUsername());
                u.put("companyname",examine.getCompanyname());
                u.put("servertype",str1);
                u.put("serverarea",str2);
                u.put("flag",examine.getFlag());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> findServerCompany(Integer id) {
        Examine examine = mapper.selectByPrimaryKey(id);
        if(examine != null){
            Map<String,Object> map = new HashMap<>();
            map.put("name",examine.getName());
            map.put("address",examine.getAddress());
            map.put("phone",examine.getPhone());
            map.put("linkman",examine.getLinkman());
            map.put("scale",examine.getScale());
            map.put("createdate",examine.getCommpanycreatedate());
            map.put("info",examine.getInfo());
            List<String> imageUrls = new ArrayList<>();
            AnnexExample annexExample1 = new AnnexExample();
            annexExample1.or().andKeyidEqualTo(id).andTypeEqualTo("2").andTabletypeEqualTo(1);
            List<Annex> annexList1 = annexMapper.selectByExample(annexExample1);
            if(!annexList1.isEmpty()){
                for(Annex annex : annexList1){
                    imageUrls.add(annex.getPath()+annex.getFilename());
                }
            }
            AnnexExample annexExample2 = new AnnexExample();
            annexExample2.or().andKeyidEqualTo(id).andTypeEqualTo("3").andTabletypeEqualTo(1);
            List<Annex> annexList2 = annexMapper.selectByExample(annexExample2);
            if(!annexList2.isEmpty()){
                Annex annex = annexList2.get(0);
                imageUrls.add(annex.getPath()+annex.getFilename());
            }
            map.put("imageUrls",imageUrls);
            return map;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> findServer(Integer id) {
        Examine examine = mapper.selectServer(id);
        if(examine != null){
            String str1 = "";
            String str2 = "";
            List<String> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            String types = examine.getServertype();
            if(types != null && !types.isEmpty()){
                String[] args1 = types.split(",");
                for(String arg:args1){
                    Type type = typeService.findById(Integer.parseInt(arg));
                    if(type != null){
                        list1.add(type.getName());
                    }
                }
                str1 = String.join(",",list1);
            }
            String areas = examine.getServerarea();
            if(areas != null && !areas.isEmpty()){
                String[] args2 = areas.split(",");
                for(String arg:args2){
                    Area area = areaService.findById(Integer.parseInt(arg));
                    list2.add(area.getName());
                }
                str2 = String.join(",",list2);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("name",examine.getName());
            map.put("companyname",examine.getCompanyname());
            map.put("address",examine.getAddress());
            map.put("phone",examine.getPhone());
            map.put("linkman",examine.getLinkman());
            map.put("servertype",str1);
            map.put("serverarea",str2);
            map.put("createdate",examine.getCreatedate());
            map.put("info",examine.getInfo());
            List<String> imageUrls = new ArrayList<>();
            AnnexExample annexExample1 = new AnnexExample();
            annexExample1.or().andKeyidEqualTo(id).andTypeEqualTo("1").andTabletypeEqualTo(1);
            List<Annex> annexList1 = annexMapper.selectByExample(annexExample1);
            if(!annexList1.isEmpty()){
                for(Annex annex : annexList1){
                    imageUrls.add(annex.getPath()+annex.getFilename());
                }
            }
            map.put("imageUrls",imageUrls);
            return map;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void examineCompany(Integer id, Integer flag, String examineInfo,Integer managerUserId) {
        Examine examine = new Examine();
        examine.setId(id);
        if(flag == 0){
            examine.setFlag(flag);
            examine.setExamineInfo(examineInfo);
            mapper.updateByPrimaryKeySelective(examine);
        }else{
            examine.setFlag(flag);
            examine.setExamineInfo(examineInfo);
            mapper.updateByPrimaryKeySelective(examine);
            Examine examine1 = mapper.selectByPrimaryKey(id);
            ServerCompany serverCompany = new ServerCompany();
            serverCompany.setName(examine1.getName());
            serverCompany.setInfo(examine1.getInfo());
            serverCompany.setAddress(examine1.getAddress());
            serverCompany.setGps(examine1.getGps());
            serverCompany.setScale(examine1.getScale());
            serverCompany.setCreatedate(examine1.getCommpanycreatedate());
            serverCompany.setJoindate(new Date());
            serverCompany.setFlag(1);
            serverCompany.setType(examine1.getTypeid());
            serverCompany.setLinkman(examine1.getLinkman());
            serverCompany.setPhone(examine1.getPhone());
            serverCompanyMapper.insertSelective(serverCompany);
            int companyId = serverCompany.getId();

            AnnexExample annexExample1 = new AnnexExample();
            annexExample1.or().andKeyidEqualTo(id).andTypeEqualTo("2").andTabletypeEqualTo(1);
            List<Annex> annexList1 = annexMapper.selectByExample(annexExample1);
            if(!annexList1.isEmpty()){
                for(Annex annex : annexList1){
                    Annex annex1 = new Annex();
                    Map<String,Object> map = null;
                    try {
                        map = OssClientUtil.uploadImage("serviceCompany/", OssClientUtil.convert(annex.getPath()+annex.getFilename()));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    String path = map.get("path").toString() + "/serviceCompany/";
                    String filename = map.get("filename").toString();
                    annex1.setPath(path);
                    annex1.setFilename(filename);
                    annex1.setType("2");
                    annex1.setKeyid(companyId);
                    annex1.setTabletype(2);
                    annexMapper.insertSelective(annex1);
                }
            }
            AnnexExample annexExample2 = new AnnexExample();
            annexExample2.or().andKeyidEqualTo(id).andTypeEqualTo("3").andTabletypeEqualTo(1);
            List<Annex> annexList2 = annexMapper.selectByExample(annexExample2);
            if(!annexList2.isEmpty()){
                Annex annex = annexList2.get(0);
                Annex annex1 = new Annex();
                Map<String,Object> map = null;
                try {
                    map = OssClientUtil.uploadImage("serviceCompany/", OssClientUtil.convert(annex.getPath()+annex.getFilename()));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                String path = map.get("path").toString() + "/serviceCompany/";
                String filename = map.get("filename").toString();
                annex1.setPath(path);
                annex1.setFilename(filename);
                annex1.setType("3");
                annex1.setKeyid(companyId);
                annex1.setTabletype(2);
                annexMapper.insertSelective(annex1);
            }

            User user = new User();
            user.setId(examine1.getUserid());
            user.setCompanyid(companyId);
            userMapper.updateByPrimaryKeySelective(user);
            Notice notice = new Notice();
            notice.setTitle("管理员操作");
            notice.setInfo("您的服务商审核通过了!");
            notice.setManageruserid(managerUserId);
            notice.setUserid(user.getId());
            notice.setFlag(1);
            notice.setCreatedate(new Date());
            noticeMapper.insertSelective(notice);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void examineServer(Integer id, Integer flag, String examineInfo, Integer managerUserId) {
        Examine examine = new Examine();
        examine.setId(id);
        if (flag == 0) {
            examine.setFlag(flag);
            examine.setExamineInfo(examineInfo);
            mapper.updateByPrimaryKeySelective(examine);
        } else {
            examine.setFlag(flag);
            examine.setExamineInfo(examineInfo);
            mapper.updateByPrimaryKeySelective(examine);
            Examine examine1 = mapper.selectByPrimaryKey(id);
            ServerInfo serverInfo = new ServerInfo();
            serverInfo.setServername(examine1.getName());
            serverInfo.setInfo(examine1.getInfo());
            serverInfo.setLinkman(examine1.getLinkman());
            serverInfo.setPhone(examine1.getPhone());
            serverInfo.setUserid(examine1.getUserid());
            User u = userMapper.selectByPrimaryKey(examine1.getUserid());
            serverInfo.setCommpanyid(u.getCompanyid());
            serverInfo.setCreatedate(new Date());
            serverInfo.setFlag(1);
            serverInfo.setPrice(examine1.getPrice());
            serverInfoMapper.insertSelective(serverInfo);
            int serverInfoId = serverInfo.getId();

            AnnexExample annexExample1 = new AnnexExample();
            annexExample1.or().andKeyidEqualTo(id).andTypeEqualTo("1").andTabletypeEqualTo(1);
            List<Annex> annexList1 = annexMapper.selectByExample(annexExample1);
            if (!annexList1.isEmpty()) {
                for (Annex annex : annexList1) {
                    Annex annex1 = new Annex();
                    Map<String, Object> map = null;
                    try {
                        map = OssClientUtil.uploadImage("service/", OssClientUtil.convert(annex.getPath() + annex.getFilename()));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    String path = map.get("path").toString() + "/service/";
                    String filename = map.get("filename").toString();
                    annex1.setPath(path);
                    annex1.setFilename(filename);
                    annex1.setType("1");
                    annex1.setKeyid(serverInfoId);
                    annex1.setTabletype(3);
                    annexMapper.insertSelective(annex1);
                }
            }

            Examine examine2 = mapper.selectServer(id);
            String types = examine2.getServertype();
            if(types != null && !types.isEmpty()){
                String[] args1 = types.split(",");
                for(String arg:args1){
                    Type type = typeService.findById(Integer.parseInt(arg));
                    if(type != null){
                        ServerType serverType = new ServerType();
                        serverType.setTypeid(type.getId());
                        serverType.setServerid(serverInfoId);
                        serverTypeMapper.insertSelective(serverType);
                    }
                }
            }
            String areas = examine2.getServerarea();
            if(areas != null && !areas.isEmpty()){
                String[] args2 = areas.split(",");
                for(String arg:args2){
                    Area area = areaService.findById(Integer.parseInt(arg));
                    if(area != null){
                        ServerArea serverArea = new ServerArea();
                        serverArea.setAreaid(area.getId());
                        serverArea.setServerid(serverInfoId);
                        serverAreaMapper.insertSelective(serverArea);
                    }
                }
            }

            Notice notice = new Notice();
            notice.setTitle("管理员操作");
            notice.setInfo("您的平台服务审核通过了!");
            notice.setManageruserid(managerUserId);
            notice.setUserid(u.getId());
            notice.setFlag(1);
            notice.setCreatedate(new Date());
            noticeMapper.insertSelective(notice);
        }
    }
}
